package com.example.accountprovider.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.accountprovider.domain.Account;
import org.apache.ibatis.annotations.Param;

/**
 * @author Kong
 * @date 2021-8-19 11:50
 */
public interface AccountMapper extends BaseMapper<Account> {
    /**
     * 扣减金额
     * @param userId
     * @param balance
     * @return
     */
    int decreaseAccount(@Param("userId") Integer userId, @Param("balance") double balance);

    /**
     * select for update 加锁
     * @param userId
     * @return
     */
    Account getAccountForUpdate(@Param("userId")Integer userId);

    /**
     * saga
     * select for update 加锁
     * @param userId
     * @return
     */
    Account getAccountForUpdateSaga(@Param("userId")Integer userId);

    /**
     * 冻结金额
     * @param userId
     * @param balance
     * @return
     */
    int updateFrozenAccount(@Param("userId")Integer userId,@Param("balance")double balance);

    /**
     * 冻结资金进行扣减
     * @param userId
     * @param balance
     * @return
     */
    int updateFrozenToUsed(@Param("userId")Integer userId,@Param("balance")double balance);

    /**
     * 回滚，将冻结资金释放
     * @param userId
     * @param balance
     * @return
     */
    int rollbackFrozen(@Param("userId")Integer userId,@Param("balance")double balance);
}
